package com.java.zhuiyun.api.member_manager_pc.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.java.zhuiyun.api.course_pc.entity.Course;
import com.java.zhuiyun.api.member_manager_pc.entity.Orders;
import com.java.zhuiyun.api.member_manager_pc.mapper.MemberManagerPcMapper;
import com.java.zhuiyun.api.member_manager_pc.service.MemberManagerPcService;
import com.java.zhuiyun.api.order.entity.FrontDeskUser;
import com.java.zhuiyun.api.order.entity.MemberType;
import com.java.zhuiyun.api.order.mapper.OrderMapper;
import com.java.zhuiyun.api.recommendedCourse.mapper.RecommendedCourseMapper;
import com.java.zhuiyun.api.recommendedCourse.model.TeacherCourseModel;
import com.java.zhuiyun.common.response.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName MemberManagerPcServiceImpl
 * @Description TODO
 * @Author wuwan
 * @Date 2023/8/28 19:58
 **/
@Service
public class MemberManagerPcServiceImpl implements MemberManagerPcService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RecommendedCourseMapper recommendedCourseMapper;

    @Autowired
    private MemberManagerPcMapper memberManagerPcMapper;

    /*
    * @Author wuwan
    * @Description 个人信息 会员中心
    * @Date 2023/8/28 21:10
    * @Return
    * @param null
    */
    @Override // 需要参数 我的会员类型 会员到期时间 折扣课程推荐 已省金额 折扣后金额
    public CommonResult getMemberManagerPc(Integer uid) {
        // 精选课程
        List<TeacherCourseModel> teacherCourseModels = memberManagerPcMapper.randonFour();
//        return  CommonResult.ok(teacherCourseModels);
        List<TeacherCourseModel> list = new ArrayList<>();
        for (int i = 0; i < teacherCourseModels.size(); i++) {
            // 用户数据 是什么会员
            FrontDeskUser frontDeskUser = orderMapper.userVip(uid);
            // 拿出用户的会员信息      也是会员的类型
            Integer frontDeskMember = frontDeskUser.getFrontDeskMember();
            // 查询出当前用户会员状态数据
            MemberType memberType = orderMapper.getMemberType(frontDeskMember);
            // 拿出当前用户会员状态的 折扣力度 百分比%
            Double discount = memberType.getDiscount();
            // 当前课程
            TeacherCourseModel teacherCourseModel = teacherCourseModels.get(i);
            // 当前课程价格
            BigDecimal coursePrice1 = teacherCourseModel.getCoursePrice();
            //类型转换 double转BigDecimal
            BigDecimal discountBD = BigDecimal.valueOf(discount);
            // 折扣后价格
            BigDecimal paymentPrice = coursePrice1.multiply(discountBD);
            // 便宜后的价格 // 原价格 - 折扣价格 = 优惠价格
            BigDecimal result = coursePrice1.subtract(paymentPrice);
            //折扣后的价格
            teacherCourseModel.setDisPrice(paymentPrice);
            // 已省金额
            teacherCourseModel.setDiscountPrice(result);
            // 会员状态
            teacherCourseModel.setFrontDeskMember(frontDeskMember);
            // 会员有效期
            teacherCourseModel.setFrontDeskMemberTime(frontDeskUser.getFrontDeskMemberTime());
            list.add(teacherCourseModel);
            if (i == teacherCourseModels.size()-1) {
                return CommonResult.ok(list);
            }
        }

        return CommonResult.error("暂无数据");
    }

    @Override
    public CommonResult getUserCourse(Integer uid,Integer orderStatus) {
        List<Orders> userCourse = memberManagerPcMapper.getUserCourse(uid,orderStatus);
        return CommonResult.ok(userCourse);
    }



    /*
    * @Author wuwan
    * @Description 个人信息课程表
    * @Date 2023/8/30 8:56
    * @Return
    * @param null
    */
    @Override
    public CommonResult getPurchasedCourse(Integer uid) {
        //查询出已购课程
        FrontDeskUser purchasedCourse = memberManagerPcMapper.getPurchasedCourse(uid);
        String purchase = purchasedCourse.getPurchase();
        if (purchase == null || purchase.equals("")) {
            return CommonResult.error("暂无课程");
        }
        List<String> purchaseList = Arrays.asList(purchase.split(","));
        List<Course> list = new ArrayList<>();

            for (int i = 0; i < purchaseList.size(); i++) {
                if (purchaseList.get(i).equals("")){
                    return CommonResult.ok();
                }else {
                    Integer s = Integer.parseInt(purchaseList.get(i));
                    Course purchased = memberManagerPcMapper.getPurchased(s);
                    if (purchased == null || purchased.equals("")){
                    }else {
                        list.add(purchased);
                    }
                }
            }
        return CommonResult.ok(list);
    }

}
